﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading;

namespace ShutdownTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public DependencyProperty obj;
        private Thread t;
        private ThreadStart ts;
        public Int32 time = 0;
        public bool threadexit = false;
        private bool shutdown = true;
        public MainWindow()
        {
            InitializeComponent();

            textBlock2.Text = createTime(0);
            
            //Create Thread for countdown!
            ts = new ThreadStart(countdown);
            t = new Thread(ts);

            Keyboard.Focus(textBox1);
            textBox1.Focus();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //Check for Plausability!!
            bool result = Int32.TryParse(textBox1.Text,out time);
            if ((true == result) && (time >= 0) && (time <= 5256000))
            {
                time *= 60;
                textBlock2.Text = createTime(time);
                
                //Start Countdown Thread
                switch (t.ThreadState)
                {
                    case System.Threading.ThreadState.Unstarted:
                        t.Start();
                        break;
                    case System.Threading.ThreadState.Stopped:
                        threadexit = false;
                        t = new Thread(new ThreadStart(countdown));
                        t.Start();
                        break;
                    default:
                        break;
                }
            }
            else
            {
                MessageBox.Show("Falsche Eingabe!\n");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Stop Shutdown Thread!
            threadexit = true;
            
            System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-a");

            // Clear previous Input and set focus backk to textBox
            textBox1.Clear(); 
            textBox1.Focus();
        }

        public void countdown()
        {
            try
            {
                for (Int32 i = time; time >= 0; time--)
                {
                    if (false == threadexit)
                    {
                        System.Threading.Thread.Sleep(1000);
                        Dispatcher.Invoke(new Action<int>(setProgresbar), time);
                    }
                    else
                    {
                        break;
                    }
                }
                //Start Shutdown!
                if (false == threadexit)
                {
                    //MessageBox.Show("Call Shutdown!");
                    if (true==shutdown)
                    {
                        System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-s -t 60");
                    }
                    else
                    {
                        System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-h");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception erkannt", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public void setProgresbar(Int32 time)
        {
            textBlock2.Text = createTime(time);
        }

        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            shutdown = false;
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            shutdown = true;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            threadexit = true;
            System.Diagnostics.Process.Start(System.Environment.SystemDirectory + "\\shutdown.exe", "-a");
        }

        private string createTime(Int32 time)
        {
            string str;
            int min = time / 60;
            int sec = time - (60*min);
            str = min.ToString() + ":" + sec.ToString();
            return str;
        }
    }
}